package com.android.mmchat.manager;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.android.mmchat.common.GloableData;




import android.content.Context;

public class SdcardManager {

	public static boolean isSDCardAvailable() {
		// String shared = "shared", // 磁盘不可�?
		// removed = "removed", // 移除
		// checking = "checking", // 正在�?��
		String mounted = "mounted";// 正常
		String temp = android.os.Environment.getExternalStorageState();
		// System.out.println("temp=="+temp);
		if (temp.equals(mounted)) {
			return true;
		} else {
			return false;
		}
	}

	public synchronized static void Download(Context ctx, String netPath,String localPath) {
		InputStream in = null;
		BufferedInputStream reader = null;
		BufferedOutputStream out = null;
		File file = null;
		try {
			if (!isSDCardAvailable()) {
				return;
			}
			if(netPath.equals("")||netPath.equals("null"))
				return;
			in = NetManagerApache.getInstance(ctx).doGetAsInputstream(netPath, null);
			reader = new BufferedInputStream(in, 1024);
			file = new File(GloableData.pictureBasePath);
			if (!file.exists())
				file.mkdirs();
			file = new File(localPath);
			if (!file.exists())
				file.createNewFile();
			out = new BufferedOutputStream(new FileOutputStream(file), 1024);
			byte[] bytes = new byte[512];
			int length = 0;
			while ((length = reader.read(bytes)) != -1) {
				out.write(bytes, 0, length);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			if (file != null)
				file.delete();
		} finally {
			try {
				if (out != null)
					out.close();
				if (reader != null)
					reader.close();
				if (in != null)
					in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}// Download

}
